package com.tiennt.android.droidcamreader;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Message;
import android.view.View;

import com.googlecode.leptonica.android.ReadFile;
import com.googlecode.tesseract.android.TessBaseAPI;

public class OcrAsyncTask extends AsyncTask<Void, Void, Boolean> {

	CameraActivity mCamActivity;
	TessBaseAPI mTessBaseApi;
	CameraActivityHandler mHandler;
	
	ProgressDialog dialog;
	
	Message msg;
	byte[] photoData;
	int width;
	int height;

	OcrResult ocrResult;

	public OcrAsyncTask(CameraActivity activity, byte[] data, int width,
			int height) {
		mCamActivity = activity;
		photoData = data.clone();
		mTessBaseApi = mCamActivity.getBaseApi();
		mHandler = mCamActivity.getHandler();

		this.width = width;
		this.height = height;

		ocrResult = new OcrResult();
	}

	@Override
	protected void onPostExecute(Boolean result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);
		dialog.dismiss();
		if (result == true) {
			msg = Message.obtain(mHandler,
					CameraActivityHandler.MSG_WCODE_OCR_RESULT_DELIVERY,
					ocrResult);
			msg.sendToTarget();
		} else {
			msg.setTarget(mHandler);
			msg.sendToTarget();
		}
	}

	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		super.onPreExecute();
		dialog  = new ProgressDialog(mCamActivity);
		dialog.setCancelable(false);
		dialog.setIndeterminate(true);
		dialog.setMessage("Processing");
		dialog.show();
	}

	@Override
	protected void onProgressUpdate(Void... values) {
		// TODO Auto-generated method stub
		super.onProgressUpdate(values);
	}

	@Override
	protected Boolean doInBackground(Void... params) {
		// TODO Auto-generated method stub
		Bitmap bitmap = mCamActivity.getCameraManager()
				.buildLuminanceSource(photoData, width, height)
				.renderCroppedGreyscaleBitmap();
		String textResult = null;

		try {
			mTessBaseApi.setImage(ReadFile.readBitmap(bitmap));
			textResult = mTessBaseApi.getUTF8Text();
			if (textResult == null || textResult.equals("")) {
				msg = mHandler.obtainMessage(
						CameraActivityHandler.MSG_WCODE_OCR_FAILED,
						"Cannot regconize.");
				return false;
			}

			ocrResult.setBitmap(bitmap);
			ocrResult.setText(textResult);
			ocrResult.setWordBoundingBoxes(mTessBaseApi.getWords()
					.getBoxRects());

		} catch (Exception ex) {
			ex.printStackTrace();
			msg = mHandler
					.obtainMessage(CameraActivityHandler.MSG_WCODE_OCR_FAILED,
							ex.getMessage());
			return false;
		}

		return true;
	}

	public static Bitmap getBitmapFromView(View view) {
		Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(),
				view.getHeight(), Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(returnedBitmap);
		Drawable bgDrawable = view.getBackground();
		if (bgDrawable != null)
			bgDrawable.draw(canvas);
		else
			canvas.drawColor(Color.WHITE);
		view.draw(canvas);
		return returnedBitmap;
	}
}
